package com.avalon;

import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 * 读取给定的{@code InputStream}和管道它直接输入到一个给定的{@code OutputStream}。
 */
public class StreamPipe implements Runnable {

	private static final Logger logger = Logger.getLogger(StreamPipe.class.getName());

	private final InputStream input;
	private final OutputStream output;

	/**
	 * 
	 * @param input
	 * @param output
	 */
	public StreamPipe(InputStream input, OutputStream output)
	{
		this.input = input;
		this.output = output;
	}

	/**
	 * 从{@code InputStream} 读取数据，放入 {@code OutputStream}
	 */
	public void run()
	{
		byte[] buf = new byte[1024];
		int count;
		try
		{
			while ((count = input.read(buf)) != -1)
			{
				output.write(buf, 0, count);
				output.flush();
			}
		} catch (IOException e)
		{
			logger.log(Level.FINEST, "Input closed", e);
		}
	}

}
